public class MySingleList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
     ListNode head;

    @Override
    public void createList(){
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);

        n1.next=n2;
        n2.next=n3;
        n3.next=n4;
        n4.next=n5;
        n5.next=n6;
        n6.next=n7;
        this.head = n1;
    }

    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        newNode.next=head;
        head=newNode;
    }

    @Override
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        ListNode cur = head;
        while(cur.next!=null){
            cur = cur.next;
        }
        cur.next=newNode;
    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        try {
            if (index < 0 || index > len) {
                throw new IndexException("下标不合法！");
            }
        } catch (IndexException e) {
            e.printStackTrace();
        }
        if(index==0){
            addFirst(data);
            return;
        }else if(index==len){
            addLast(data);
            return;
        }else{
            ListNode cur = head;
            for (int i = 0; i < index-1; i++) {
                cur=cur.next;
            }
            ListNode newNode = new ListNode(data);
            newNode.next=cur.next;
            cur.next=newNode;
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode  cur = head;
        while(cur!=null){
            if(cur.val==key)
                return true;
            cur=cur.next;
        }
        return false;
    }

    ListNode findListNode(int key){
        ListNode cur = head;

        while(cur.next!=null){
            if(cur.next.val == key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }

    @Override
    public void remove(int key) {
        if(head==null)
            return;
        if(head.val==key){
            head=head.next;
            return;
        }
        ListNode cur = findListNode(key);
        if(cur==null)
            return;
        ListNode del = cur.next;
        cur.next=del.next;
    }

    @Override
    public void removeAllKey(int key) {
        if(head==null){
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur!=null){
            if(cur.val==key) {
                cur = cur.next;
                prev.next = cur;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }

        if(head.val==key){
            head=head.next;
        }
    }


    @Override
    public int size() {
        int cnt = 0;
        ListNode cur = head;
        while(cur!=null){
            cnt++;
            cur=cur.next;
        }
        return cnt;
    }

    @Override
    public void clear() {
        ListNode cur = head.next;
        ListNode curNext = cur.next;
        while(cur.next!=null){
            cur.next=null;
            cur=curNext;
            curNext=cur.next;
        }
        head=null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println("  ");
    }

}
